//电子词典客户端

#include "head.h"
#define text_max 256
#define name_max 256

typedef struct message
{
	int type;
	char name[name_max];
	char data[text_max];
}MSG;

void do_search(int sockfd);
void do_register(int sockfd);
void do_login(int sockfd);
int main(int argc, const char *argv[])
{
	int fd[2];
	char str3 = 'e';
	char str2 = 's';
	if(pipe(fd)<0)
	{
		perror("pipe err.");
		return -1;
	}
	int sockfd;
	char mode_pid2[name_max];
	char mode[name_max];//用来接受用户菜单命令
	sockfd = socket(AF_INET,SOCK_STREAM,0);
	if(sockfd < 0)
	{
		perror("sockfd err.");
		return -1;
	}

	struct sockaddr_in serveraddr;
	struct sockaddr_in clientaddr;
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_port = htons(atoi(argv[2]));
	serveraddr.sin_addr.s_addr = inet_addr(argv[1]);

	socklen_t serlen = sizeof(serveraddr);
	socklen_t clilen = sizeof(clientaddr);

	if(connect(sockfd,(struct sockaddr*)&serveraddr,serlen)<0)
	{
		perror("bind err.");
		return -1;
	}
	printf("字典连接成功\n");
pro1:
	//打印菜单
	printf("********************************\n");
	printf("********************************\n");
	printf("** 1.register 2.login 3.quit ***\n");
	printf("********************************\n");
	printf("********************************\n");
	pid_t pid;

	pid = fork();
	if(pid < 0)
	{
		perror("pid err.");
		return -1;
	}
	else if(pid == 0)
	{
		ssize_t recvbyte;
		MSG msg;
		char temp;
		char *namedb; 
		while(1)
		{
			write(fd[1],&str3,1);
			recvbyte = recv(sockfd,&msg,sizeof(MSG),0);
			if(recvbyte < 0)
			{
				perror("client pid = 0 recv err.");
				return -1;
			}
			switch(msg.type)
			{
			case 'L'://用户登录
				if(msg.data[0] == 'w')
				{
					printf("用户密码输入错误或未注册!\n");
				}
				else if(msg.data[0] == 's')
				{
					write(fd[1],&str2,1);
					printf("登录成功！\n");
					
				}
				break;
			case 'Q'://用户退出
				exit(0);
				break;
			case 'S':
				printf("%s\n",msg.data);
				break;
			case 'H':
				printf("-----history---\n");
				break;
			case 'R'://用户注册
				printf("注册成功!\n");
				printf("用户名:%s\n",msg.name);
				printf("密码:%s\n",msg.data);
				break;
			case 'B'://服务器广播
				printf("%s\n",msg.data);
				break;
			default:
				printf("server return err.");
				break;
			}

		}
	}
	/*-----------------------父进程1发送命令------------------------------*/
	else
	{
		char rec;
		int n = 1;
		while(1)
		{
			printf("please choose:>");
mode_input:
			fgets(mode,name_max,stdin);  //接收命令
			switch(mode[0])
			{
			case 'r':
				do_register(sockfd);
				break;
			case 'l':
				do_login(sockfd);
				break;
			case 'q':
				goto quit1;
				break;
			default:
				printf("输入错误，请重新输入:>");
				goto mode_input;
				break;
			}
			sleep(1);
			read(fd[0],&rec,1);
			read(fd[0],&rec,1);
			printf("%c\n",rec);
			/*-------------------------二级菜单---------------------------------*/
			if(strncmp(&rec,"s",1)==0)
			{
			printf("*************************************************\n");
			printf("*************************************************\n");
			printf("*************************************************\n");
			printf("********        欢迎使用本电子词典      *********\n");
			printf("********         1.查单词(search)       *********\n");
			printf("********         2.历史记录(history)    *********\n");
			printf("********         3.退出(quit)           *********\n");
			printf("*************************************************\n");
			printf("*************************************************\n");
			printf("*************************************************\n");
			
			while(1)
			{
				sleep(1);
				printf("请输入命令：>");
				fgets(mode_pid2,name_max,stdin);
				switch(mode_pid2[0])
				{
				case 's':
					printf("------------search----------\n\n");
					do_search(sockfd);
					break;
				case 'h':
					printf("------------history----------\n\n");
					//do_history();
					break;
				case 'q':
					printf("------------quit-------------\n\n");
					goto pro1;
					break;
				default:
					printf("2:input err.\n");
					break;
				}
			}
		}
	}
}
quit1:
	printf("再见!!~~~~~~~~~\n");
	close(sockfd);
	close(fd[0]);
	close(fd[1]);
	return 0;
}
//注册函数 
void do_register(int sockfd)
{
	MSG msg;
	msg.type = 'R';
	printf("******  register   ******\n");
	printf("请输入您要注册的账户名:>");
	fgets(msg.name,name_max,stdin);
	if(msg.name[strlen(msg.name)-1]== '\n')
		msg.name[strlen(msg.name)-1] == '\0';
	printf("请输入您的用户密码:>");
	fgets(msg.data,text_max,stdin);
	if(msg.data[strlen(msg.data)-1]== '\n')
		msg.data[strlen(msg.data)-1]=='\0';
	//printf("%s\n",msg.name);
	//printf("%s\n",msg.data);
	send(sockfd,&msg,sizeof(msg),0);	
}

void do_login(int sockfd)
{
	MSG msg;
	msg.type = 'L';
	printf("*****  login   *****\n");
	printf("请输入账户名:>");
	fgets(msg.name,name_max,stdin);
	if(msg.name[strlen(msg.name)-1] == '\n')
		msg.name[strlen(msg.name)-1] == '\0';

	printf("请输入密码:>");
	fgets(msg.data,text_max,stdin);
	if(msg.data[strlen(msg.data)-1]== '\n')
		msg.data[strlen(msg.data)-1]=='\0';
	send(sockfd,&msg,sizeof(msg),0);
	printf("登录信息正在发送\n");
}

void do_search(int sockfd)
{
	MSG msg;
	msg.type = 'S';
	printf("请输入你要查询的单词:>");
	fgets(msg.name,name_max,stdin);
	if(msg.name[strlen(msg.name)-1] == '\n')
	msg.name[strlen(msg.name)-1]='\0';
	send(sockfd,&msg,sizeof(MSG),0);
}
